package com.chang.multithread;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.locks.LockSupport;

@Slf4j  //如果主线程先执行LockSupport.unpark();  后面线程执行LockSupport.park();也会被唤醒
public class Park {
    public static void main(String[] args) throws Exception {
        test();
    }
    public static void test() throws Exception{
        Thread t1=new Thread(()->{
            log.debug("park");
            LockSupport.park();
            log.debug("unpark....");
            //log.debug("打断状态：{}",Thread.currentThread().isInterrupted());//不会清除打断状态
            log.debug("打断状态：{}",Thread.interrupted());//会清除打断状态

            LockSupport.park();//打断标记为true的时候，这个方法就失效，继续执行下面的内容
            log.debug("unpark....");
        },"t1");
        t1.start();


        Thread.sleep(1000);
        t1.interrupt();
    }

    @Test
    void park() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            log.debug("准备运行！！！");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("准备暂停");
            LockSupport.park();
            log.debug("t1 恢复运行！！！");
        }, "t1");
        t1.start();

        Thread.sleep(1250);
        log.debug("终止t1的暂停");
        LockSupport.unpark(t1);
    }
}
